package com.ktwlsoft.archivemanage.controller;


import com.ktwlsoft.archivemanage.domain.business.businessService.QuestionDicService;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.question.QuestionDicSaveRequest;
import com.ktwlsoft.archivemanage.payload.question.QuestionDicUpdateRequest;
import com.ktwlsoft.archivemanage.payload.question.RelatedQuestionRequest;
import com.ktwlsoft.archivemanage.domain.dao.entity.QuestionDic;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(value = "问题字典", description = "问题字典")
@RestController
@RequestMapping("/questiondic/manage")
public class QuestionDicController {
    /**
     * 问题字典
     */
    private final QuestionDicService questionDicService;

    public QuestionDicController(QuestionDicService questionDicService) {
        this.questionDicService = questionDicService;
    }

    /**
     * 问题字典分页
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "问题字典分页", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("list")
    public ResponseEntity<?> list(@RequestParam(defaultValue = "1") int pn,
                                  @RequestParam(defaultValue = "30") int pageSize,
                                  @ApiParam(name = "businessFrom", value = "业务来源") @RequestParam(defaultValue = "", required = true) String businessFrom
    ) {
        return ResponseEntity.ok(questionDicService.findPage(pn, pageSize, businessFrom));
    }

    /**
     * 问题字典新增
     *
     * @param request
     * @param bindingResult
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "问题字典新增", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("add")
    public ResponseEntity<?> add(@Valid @RequestBody QuestionDicSaveRequest request) {
        Result result = questionDicService.save(request);
        return ResponseEntity.ok(result);
    }

    /**
     * 问题字典修改
     *
     * @param request
     * @param bindingResult
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "问题字典修改", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("edit")
    public ResponseEntity<?> edit(@Valid @RequestBody QuestionDicUpdateRequest request) {
        Result result = questionDicService.edit(request);
        return ResponseEntity.ok(result);
    }

    /**
     * 问题字典启用禁用
     * <p>
     * * @param id id
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "问题字典启用禁用", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "enable")
    public ResponseEntity<?> enable(@ApiParam(name = "id", value = "问题字典id") @RequestParam(defaultValue = "", required = true) Long id) {
        Result result = questionDicService.enableById(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 问题字典删除
     *
     * @param id id
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "问题字典删除", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("delete")
    public ResponseEntity<?> delete(@ApiParam(name = "id", value = "问题字典id") @RequestParam(defaultValue = "", required = false) Long id) {
        Result result = questionDicService.deleteById(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 问题导航下的问题列表
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "问题导航下的问题列表")
    @GetMapping("/relatedQuestions")
    public ResponseEntity<?> relatedQuestions(RelatedQuestionRequest request) {
        return ResponseEntity.ok(questionDicService.getByCategoryId(request));
    }
}
